package com.htgd.gardenexpo.utils;
import com.alibaba.fastjson.JSONObject;
import org.springframework.http.ResponseEntity;
import org.springframework.web.client.RestTemplate;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.MediaType;

/**
 * RestTemplate发送HTTP工具类
 * @author lihailong
 * @date
 */
public class RestTemplateUtils {

    // 创建 RestTemplate 实例
    private static RestTemplate restTemplate = new RestTemplate();

    // 发送 GET 请求
    public static JSONObject sendGetRequest(String url) {
        return restTemplate.getForObject(url, JSONObject.class);
    }

    // 发送 POST 请求
    public static String sendPostRequest(String url, String jsonBody) {
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);
        HttpEntity<String> entity = new HttpEntity<>(jsonBody, headers);

        ResponseEntity<String> response = restTemplate.exchange(url, HttpMethod.POST, entity, String.class);
        return response.getBody();
    }

    public static void main(String[] args) {
        // 示例GET请求
        String getUrl = "https://zg.yhxxp.com/ibms/prod-api/open/park/realtime";
        JSONObject getResponse = sendGetRequest(getUrl);
        System.out.println("GET response: " + getResponse);

//        // 示例POST请求
//        String postUrl = "https://jsonplaceholder.typicode.com/posts";
//        String postBody = "{\"title\": \"foo\", \"body\": \"bar\", \"userId\": 1}";
//        String postResponse = sendPostRequest(postUrl, postBody);
//        System.out.println("POST response: " + postResponse);
    }
}
